package com.wkf.truck.controller;


import com.alibaba.fastjson.serializer.StringCodec;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.org.apache.bcel.internal.classfile.Code;
import com.wkf.truck.common.CommonEnum;
import com.wkf.truck.common.R;
import com.wkf.truck.model.Distance;
import com.wkf.truck.service.DistanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.activation.CommandMap;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lxb
 * @since 2021-03-11
 */
@RestController
@RequestMapping("/distance")
public class DistanceController {

    @Autowired
    private DistanceService distanceService;

    /**
     * 获取价格
     *
     * @param fromCode
     * @param toCode
     * @param truckCode
     * @return
     */
    @GetMapping("/getMoney")
    public R getMoney(@RequestParam("fromCode") String fromCode, @RequestParam("toCode") String toCode, @RequestParam("truckCode") String truckCode) {

        String code = fromCode + toCode;

        Distance distance = distanceService.getOne(new QueryWrapper<Distance>().eq("code", code));

        if (distance == null) {
            return R.error("查询数据库失败");
        }

        //单价
        int price = 0;

        if (truckCode.equals(CommonEnum.TruckType.小面包.getCode())) {
            price =5;
        } else if (truckCode.equals(CommonEnum.TruckType.中面包.getCode())) {
            price =10;
        } else if (truckCode.equals(CommonEnum.TruckType.小货车.getCode())) {
            price =15;
        } else if (truckCode.equals(CommonEnum.TruckType.中箱货.getCode())) {
            price = 20;
        } else if (truckCode.equals(CommonEnum.TruckType.中平板.getCode())) {
            price = 25;
        } else if (truckCode.equals(CommonEnum.TruckType.五米二.getCode())) {
            price =30;
        } else if (truckCode.equals(CommonEnum.TruckType.六米八.getCode())) {
            price = 35;
        }

        double cost = price * distance.getDistance().doubleValue();

        return R.ok(cost);
    }
}
